#include <dao.h>
#include <btBulletDynamicsCommon.h>

#include "btMotionState.h"
#include "btRigidBody.h"

static void construct(DaoContext *ctx, DValue *p[], int N) {
    btRigidBody *ptr;
    btRigidBody::btRigidBodyConstructionInfo *constructionInfo;

    constructionInfo = (btRigidBody::btRigidBodyConstructionInfo*) DaoCData_GetData(p[0]->v.cdata);
    ptr = new btRigidBody(*constructionInfo);

    DaoCData *cdata = DaoContext_WrapCData(ctx, ptr, &btRigidBodyTyper);
};

static void destruct(void *self) {
//    delete (btRigidBody*) self;
}

static void getMotionState(DaoContext *ctx, DValue *p[], int N) {
    btRigidBody		*ptr = (btRigidBody*) DaoCData_GetData(p[0]->v.cdata);
    btMotionState	*res = ptr->getMotionState();

    DaoContext_PutCData(ctx, res, &btMotionStateTyper);
}

static void setFriction(DaoContext *ctx, DValue *p[], int N) {
    btRigidBody		*ptr = (btRigidBody*) DaoCData_GetData(p[0]->v.cdata);

    ptr->setFriction(p[1]->v.f);
}

static void setDamping(DaoContext *ctx, DValue *p[], int N) {
    btRigidBody		*ptr = (btRigidBody*) DaoCData_GetData(p[0]->v.cdata);

    ptr->setDamping(p[1]->v.f, p[2]->v.f);
}

static void setRestitution(DaoContext *ctx, DValue *p[], int N) {
    btRigidBody		*ptr = (btRigidBody*) DaoCData_GetData(p[0]->v.cdata);

    ptr->setRestitution(p[1]->v.f);
}

static DaoFuncItem meth[] = {
    { construct,	"btRigidBody(constructionInfo:btRigidBodyConstructionInfo)" },
    { getMotionState,	"getMotionState(self:btRigidBody) => btMotionState" },
    { setFriction,	"setFriction(self:btRigidBody, fric:float)" },
    { setDamping,	"setDamping(self:btRigidBody, lin:float, ang:float)" },
    { setRestitution,	"setRestitution(self:btRigidBody, rest:float)" },
    { NULL, NULL }
};

DaoTypeBase btRigidBodyTyper = {
    "btRigidBody",
    NULL,
    NULL,               /* constant */
    meth,       	/* methods */
    { NULL },     	/* parent type */
    { NULL },
    destruct,
    NULL
};
